﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RealtorsPortal.Entities;
using RealtorsPortal.Data_Access_Layer;

namespace RealtorsPortal
{
    public partial class AdminTemplate : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.CheckIsLogged();
            lblUser.Text = Session["adminLogin"].ToString();
            lblRoleName.Text = Session["adminRole"].ToString();
            this.SetDisplayByRole();
            lblUsername.Text = lblUser.Text;
        }

        private string GetCurrentPageName()
        {
            string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
            string sRet = oInfo.Name;
            return sRet;
        }


        private void CheckIsLogged()
        {
            if (Request.Cookies["AdminLogin"] != null)
            {
                HttpCookie ckLogin = Request.Cookies["AdminLogin"];
                Session["adminLogin"] = ckLogin["adminLogin"];
                Session["adminId"] = ckLogin["adminId"];
                Session["adminRole"] = ckLogin["adminRole"];
            }

            if (Session["adminLogin"]==null)
            {
                Response.Redirect("~/AdminCP/Login.aspx?ReturnUrl=" + this.GetCurrentPageName());
            }
        }
        
        

        private void SetDisplayByRole() {
            string role = Session["adminRole"].ToString();
            switch(role){
                case "Administrator" :
                    navheadAdmin.Visible = true;
                    mnuAdmin.Visible = true;

                    navheadADV.Visible = false;
                    mnuAdv.Visible = false;

                    navheadAgents.Visible = false;
                    mnuAgents.Visible = false;

                    navheadProfiles.Visible = false;
                    mnuProfiles.Visible = false;

                    navheadReports.Visible = true;
                    mnuReports.Visible = true;

                    navheadSettings.Visible = true;                   
                    mnuSettings.Visible = true;
                    break;
                case "Agent":
                    navheadAdmin.Visible = false;
                    mnuAdmin.Visible = false;

                    navheadADV.Visible = true;
                    mnuAdv.Visible = true;

                    navheadAgents.Visible = true;
                    mnuAgents.Visible = true;

                    navheadProfiles.Visible = true;
                    mnuProfiles.Visible = true;

                    navheadReports.Visible = false;
                    mnuReports.Visible = false;

                    navheadSettings.Visible = false;                   
                    mnuSettings.Visible = false;
                    break;
                default:
                    navheadAdmin.Visible = false;
                    mnuAdmin.Visible = false;

                    navheadADV.Visible = true;
                    mnuAdv.Visible = true;

                    navheadAgents.Visible = false;
                    mnuAgents.Visible = false;

                    navheadProfiles.Visible = true;
                    mnuProfiles.Visible = true;

                    navheadReports.Visible = false;
                    mnuReports.Visible = false;

                    navheadSettings.Visible = false;                   
                    mnuSettings.Visible = false;
                    break;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string role = Session["adminRole"].ToString();
            int userid = int.Parse(Session["adminId"].ToString());
            string newpass =Utilities.GetMD5HashData(txtNewPassword.Text.Trim());
            bool result = false;
            if (role == "Administrator")
            {
                result = new AdministratorDAL().ChangePassword(userid, newpass);
            }
            else
            {
                result = new AccountDAL().ChangePassword(userid, newpass);
            }

            if (result)
            {
                Response.Write("<script type='text/javascript'>alert('Change password successfully');</script>");
            }
            else
            {
                Response.Write("<script type='text/javascript'>alert('An error occur when chaging password');</script>");
            }
        }
         
    }
}